package com.hspedu.newData.dmsxl.链表.翻转链表;

import com.hspedu.newData.dmsxl.ds.ListNode;
import org.junit.Test;

/**
 * @Author: yh
 * @description: 
 * @CreateTime: 2025-04-22 
 * @Version: 1.0
 */

public class LC92反转链表Ⅱ {

    // todo
    // 反转链表2
    public ListNode reverseBetween(ListNode head, int left, int right) {
        ListNode dummy = new ListNode();
        dummy.next = head;
        ListNode cur = dummy;
        for (int i = 1; i < left; i++) {
            cur = cur.next;
        }
        ListNode s1 = cur;
        cur = cur.next;
        ListNode prev = null;
        for (int i = left; i <= right; i++) {
            ListNode temp = cur.next;
            cur.next = prev;
            prev = cur;
            cur = temp;
        }
        s1.next = prev;
        while(prev.next != null){
            prev = prev.next;
        }
        prev.next = cur;
        return dummy.next;

    }


    public ListNode reverse(ListNode head){
        ListNode prev = null;
        ListNode cur = head;

        while(cur!=null){
            ListNode temp = cur.next;
            cur.next = prev;
            prev = cur;
            cur = temp;
        }
        return prev;
    }



    @Test
    public void test() {
        // 测试代码
//        ListNode l1 = new ListNode(6, null);
        ListNode l2 = new ListNode(5, null);
        ListNode l3 = new ListNode(4, l2);
        ListNode l4 = new ListNode(3, l3);
        ListNode l5 = new ListNode(2, l4);
        ListNode l6 = new ListNode(1, l5);

        reverseBetween(l6,2,4);

        System.out.println("111");
    }
}
